from Tkinter import *
from PIL import Image,ImageTk
from modelo.Serie import Serie
from servicios.Persistense import Persistense

class FrameSerieInfo(Frame):

    _yScrollSeasonsSeasons = None
    _seasons_list = None
    _seasons = []
    _chapters_list = None
    _chapters = []
    _yScrollChapters = None

    _button_seguir = None

    _serie = None
    _season = None
    _chapter = None

    _saved_data = None
    
    def __init__(self, master, idSerie):
        Frame.__init__(self,master)

        self._serie = Serie.get_instance(idSerie)

        self._saved_data = Persistense.instance().get_serie(self._serie.get_id())
        if self._saved_data is None:
            self._saved_data = {'id':'', 'season':'', 'chapter':''}
        
        photo = Image.open(self._serie.get_image())
        self._logo = ImageTk.PhotoImage(photo)
        self._canvas = Canvas (self, width=120, height=160)
        self._canvas.create_image(0,0,image=self._logo, anchor=NW)
        self._canvas.grid(column=2, row=0)

        self._yScrollSeasons  =  Scrollbar ( self, orient=VERTICAL )
        self._yScrollSeasons.grid ( row=0, column=4, sticky=N+S )
        self._seasons_list = Listbox (self, yscrollcommand=self._yScrollSeasons.set)
        self._seasons_list.grid(row = 0, column = 3,sticky=N+S+E+W)
        self._seasons = sorted(self._serie.get_seasons().values(),key=lambda s: s.get_number())
        self._seasons_list.bind('<ButtonRelease-1>', self.selected_season)
        self._yScrollSeasons["command"]  =  self._seasons_list.yview

        self._yScrollChapters  =  Scrollbar ( self, orient=VERTICAL )
        self._yScrollChapters.grid ( row=0, column=6, sticky=N+S )
        self._chapters_list = Listbox (self, yscrollcommand=self._yScrollChapters.set)
        self._chapters_list.grid(row = 0, column = 5,sticky=N+S+E+W)
        self._yScrollChapters["command"] = self._chapters_list.yview
        self._chapters_list.bind('<ButtonRelease-1>', self.selected_chapter)

        self.load_seasons_list()
        if self._saved_data['season'] != '':
            self._seasons_list.index(int(self._saved_data['season']) -1)
            self._seasons_list.see(int(self._saved_data['season']) -1)

            self.load_default_chapter()
        else:
            self._seasons_list.see(0)
            self._seasons_list.index(0)

    def load_default_chapter(self):
        self._season = self._serie.get_season(self._saved_data['season'])
        self._chapters = sorted(self._season.get_chapters().values(),key=lambda c: c.get_number())
        self.load_list()
        
        if self._saved_data['chapter'] != '':
            self._chapters_list.index(int(self._saved_data['chapter']) -1)
            self._chapters_list.see(int(self._saved_data['chapter']) -1)
        else:
            self._chapters_list.see(0)
            self._chapters_list.index(0)
		
    def load_seasons_list(self):
        for season in self._seasons:
            prefije = ''
            if (season.get_number() == self._saved_data['season']):
                prefije = '>'
            self._seasons_list.insert (END, prefije + 'Temporada ' + str(season.get_number()))
	
    def selected_season(self, event):
        selected = int(self._seasons_list.curselection()[0])
        self._season = self._serie.get_season(selected+1)
        self._chapters = sorted(self._season.get_chapters().values(),key=lambda c: c.get_number())
        self.load_list()
        
    def load_list(self):
        self._chapters_list.delete(0,END)
        for c in self._chapters:
            prefije = ''
            if (self._season.get_number() == self._saved_data['season'] and c.get_number() == self._saved_data['chapter']):
                prefije = '>'
            self._chapters_list.insert (END, prefije + str(c.get_number()) + ' - ' + c.get_name())
	
    def selected_chapter(self, event):
        selected = int(self._chapters_list.curselection()[0])
        self._chapter = self._chapters[selected]

        imagen = PhotoImage (file=Persistense.instance().get_app_path() + 'img/seguir.gif')
        
        self._button_seguir = Button(self, image=imagen, command = self.menu_seguir_click)
        self._button_seguir.imagen = imagen
        self._button_seguir.grid(column=5, row=1, sticky=E)
	
    def menu_seguir_click(self):
        p = Persistense.instance()
        p.add_serie(self._serie.get_id(), self._season.get_number(), self._chapter.get_number())
        p.save()

        self._chapters_list.delete(0,END)
        self.load_list()
        self._chapters_list.see(self._chapter.get_number()-1)
